package com.sztx.pay.center.common.util;


import java.util.regex.Pattern;

import org.apache.commons.validator.routines.EmailValidator;
import org.apache.commons.validator.routines.InetAddressValidator;
import org.apache.commons.validator.routines.UrlValidator;

import com.sztx.se.common.exception.BusinessException;



public class ValidateUtil extends com.sztx.se.common.util.validate.ValidateUtil {
	
	/**
	 * 断言是否是正确的ip地址，否在抛出Bussiness异常
	 * 
	 * @param ipAddress
	 * @param message
	 * @author xiaosc
	 * @date 2016-1-13 上午10:42:49 
	 * @see [类、类#方法、类#成员]
	 */
	public static void isInet4Address(String ipAddress, String message){
		
		isNotBlank(ipAddress, message);
		
		InetAddressValidator validator = new InetAddressValidator();
		
		if (!validator.isValidInet4Address(ipAddress)) {
			throw new BusinessException(message);
		}	
	}
	
	/**
	 *  断言是数字是否在区间内，否在抛出Bussiness异常
	 * 
	 * @param value
	 * @param min
	 * @param max
	 * @param message
	 * @author xiaosc
	 * @date 2016-1-13 上午11:12:46 
	 * @see [类、类#方法、类#成员]
	 */
	public static void isBetween(int value, int min, int max, String message){
		if (value < min || value > max) {
			throw new BusinessException(message);
		}
	}
	
	/**
	 * 断言是否正确的url，否在抛出Bussiness异常
	 * <功能详细描述>
	 * @param url
	 * @param message
	 * @author xiaosc
	 * @date 2016-1-13 上午11:19:19 
	 * @see [类、类#方法、类#成员]
	 */
	public static void isUrl(String url, String message){
		isNotBlank(url, message);
		
		UrlValidator validator = new UrlValidator();
		
		if (!validator.isValid(url)) {
			throw new BusinessException(message);
		}	
	}
	
	/**
	 * 断言是否正确的email，否在抛出Bussiness异常
	 * <功能详细描述>
	 * @param url
	 * @param message
	 * @author xiaosc
	 * @date 2016-07-12 下午07:35:19 
	 * @see [类、类#方法、类#成员]
	 */
	public static void isEmail(String email, String message){		
		isNotBlank(email, message);
		
		if (!EmailValidator.getInstance().isValid(email)) {
			throw new BusinessException(message);
		}
		
	}
	
	
	/**
	 * 断言是否正确的手机号，否在抛出Bussiness异常
	 * 
	 * @param mobile
	 * @param message
	 * @author xiaosc
	 * @date 2016-1-13 下午3:11:14 
	 * @see [类、类#方法、类#成员]
	 */
	public static void isMobile(String mobile, String message) {
		
		isMatch(mobile, "^[1][0-9]{10}$", message);
	}
	
	/**
	 * 根据正则表达式断言
	 * @param text
	 * @param pattern
	 * @param message
	 * @author xiaosc
	 * @date 2016-1-13 下午3:11:46 
	 * @see [类、类#方法、类#成员]
	 */
	public static void isMatch(String text,  String pattern, String message) {
        isNotBlank(text, message);
        if (!Pattern.matches(pattern, text))
        {
        	throw new BusinessException(message);
        }
    }
	
	public static void gt(Long theNum, Long diff, String message) {
		if (theNum == null || diff == null) {
			throw new BusinessException(message);
		}

		if (theNum <= diff) {
			throw new BusinessException(message);
		}
	}
	
	public static void ge(Long theNum, Long diff, String message) {
		if (theNum == null || diff == null) {
			throw new BusinessException(message);
		}

		if (theNum < diff) {
			throw new BusinessException(message);
		}
	}

}
